<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="管理员" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree default-expand-all :data="data" :props="defaultProps">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template slot-scope="{ node }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
    >
      <DeptDialog
        v-if="showDialog"
        :pid="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @close="hClose"
        @success="hSuccess"
      />
    </el-dialog>
  </div>
</template>
<!-- 部门管理， 里边有一个弹层 -->
<script>
import { arrayToTree } from '@/utils/index'
import { getDepartment, delDepartment } from '@/api/departments'
import DeptDialog from './deptDialog.vue'
export default {
  components: { DeptDialog },
  data() {
    return {
      curId: '', // 当前选中的部门ID
      showDialog: false, // 是否显示弹层
      // 保存数据
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isEdit: false, // 是否处于编辑状态
      originList: []
    }
  },
  created() {
    // 发请求
    this.loadDepartment()
  },

  methods: {
    hClose() {
      this.showDialog = false
    },
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartment()
    },
    // 用户点击了编辑
    hEdit(id) {
      // 1. 弹层出来
      this.showDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前操作的id
      console.log(id)
      this.curId = id
      // 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = false
    },
    async doDel() {
      await delDepartment()
      this.$message.success('删除成功!')
      this.$message.error('删除失败!')
      this.loadDepartment()
    },
    hDel(id) {
      this.$confirm('你确定要删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    // 加载数据
    async loadDepartment() {
      const res = await getDepartment()
      console.log('res', res)
      // 收集数据提供给子组件做表单校验时，使用
      this.originList = res.data.depts.map((item) => {
        return {
          id: item.id,
          name: item.name,
          code: item.code,
          pid: item.pid
        }
      })
      // 去掉第一项
      res.data.depts.shift()
      // 数组转成树
      this.data = arrayToTree(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
